@echo off
echo ========================================
echo 等待所有微服务完全启动
echo ========================================

echo 当前时间: %date% %time%
echo.

echo ========================================
echo 监控服务启动状态
echo ========================================
echo 等待所有Spring Boot服务完全启动（最多5分钟）...
set /a wait_time=0
set /a max_wait=300

:monitor_loop
echo.
echo 检查服务状态... (等待 %wait_time% 秒)
docker-compose ps

echo.
echo ========================================
echo 测试各服务健康检查
echo ========================================

echo 测试API网关...
curl -s http://localhost:8080/actuator/health >nul 2>&1
if errorlevel 1 (
    echo ❌ API网关未就绪
) else (
    echo ✅ API网关已就绪
)

echo 测试用户管理服务...
curl -s http://localhost:8082/actuator/health >nul 2>&1
if errorlevel 1 (
    echo ❌ 用户管理服务未就绪
) else (
    echo ✅ 用户管理服务已就绪
)

echo 测试课程管理服务...
curl -s http://localhost:8083/actuator/health >nul 2>&1
if errorlevel 1 (
    echo ❌ 课程管理服务未就绪
) else (
    echo ✅ 课程管理服务已就绪
)

echo 测试练习管理服务...
curl -s http://localhost:8085/actuator/health >nul 2>&1
if errorlevel 1 (
    echo ❌ 练习管理服务未就绪
) else (
    echo ✅ 练习管理服务已就绪
)

echo 测试学习进度服务...
curl -s http://localhost:8084/actuator/health >nul 2>&1
if errorlevel 1 (
    echo ❌ 学习进度服务未就绪
) else (
    echo ✅ 学习进度服务已就绪
)

echo.
echo 检查所有服务是否就绪...
curl -s http://localhost:8080/actuator/health >nul 2>&1
set gateway_ready=%errorlevel%

curl -s http://localhost:8082/actuator/health >nul 2>&1
set user_ready=%errorlevel%

curl -s http://localhost:8083/actuator/health >nul 2>&1
set course_ready=%errorlevel%

curl -s http://localhost:8085/actuator/health >nul 2>&1
set exercise_ready=%errorlevel%

curl -s http://localhost:8084/actuator/health >nul 2>&1
set progress_ready=%errorlevel%

if %gateway_ready%==0 if %user_ready%==0 if %course_ready%==0 if %exercise_ready%==0 if %progress_ready%==0 (
    echo.
    echo 🎉 所有服务已就绪！
    goto all_ready
)

set /a wait_time+=30
if %wait_time% geq %max_wait% (
    echo.
    echo ❌ 等待超时，部分服务可能有问题
    goto check_logs
)

echo.
echo 继续等待... (已等待 %wait_time% 秒)
timeout /t 30 /nobreak >nul
goto monitor_loop

:all_ready
echo.
echo ========================================
echo 验证服务注册状态
echo ========================================
echo 检查Consul服务注册...
curl -s http://localhost:8500/v1/catalog/services

echo.
echo 测试数据库连接...
docker exec microservices-mysql-1 mysql -uroot -proot123 -e "SELECT 1;" 2>nul
if errorlevel 1 (
    echo ❌ 数据库连接失败
) else (
    echo ✅ 数据库连接正常
)

echo.
echo ========================================
echo 所有服务启动完成！
echo ========================================

echo 📋 现在可以运行测试:
echo 1. 基础测试: test-system.bat
echo 2. API功能测试: test-api-endpoints.bat
echo 3. 业务流测试: test-business-flow.bat
echo 4. 完整测试: run-e2e-tests.bat
echo.

goto end

:check_logs
echo.
echo ========================================
echo 检查服务日志
echo ========================================
echo 检查用户管理服务日志...
docker-compose logs --tail=10 user-management-service

echo.
echo 检查课程管理服务日志...
docker-compose logs --tail=10 course-management-service

:end
pause
